<script setup>
import { updateImageColors, preloadImage } from '@/utils/tool.js';
import { choiceYearData } from '@/stores/performanceData.js';

/** 初始化背景颜色 */
let bgColorsString = ref('');
let currIndex = ref(0);
(async () => {
  bgColorsString.value = await updateImageColors(`https://kanekikeh.online/A/CoursePerformance/timeLineImg/${choiceYearData[currIndex.value].image}`);
  bgColorsString.value.join(', ')
})();

/** 事件总线传递信息 */
const cxt = getCurrentInstance() // context === vue2 this
const bus = cxt.appContext.config.globalProperties.$bus
const userChoice = ((val) => { bus.emit('Performance', val) })
const closeTemplate = (() => { bus.emit('closeTemplate') })

/** 动画效果 */
const margin = 20;
const itemWidth = computed(() => Math.max(window.innerWidth * 0.1, 400) - margin * 2);
const itemHeight = computed(() => window.innerHeight * 0.5);
const totalWidth = computed(() => itemWidth.value * choiceYearData.length);
const sliderTransform = computed(() => `translate3d(${currIndex.value * -itemWidth.value + itemWidth.value / 2 + window.innerWidth / 2}px, 0, 0)`);

function itemTransform(index) {
  if (index === currIndex.value) {
    return 'perspective(1200px)';
  } else {
    return `perspective(1200px) rotateY(${index < currIndex.value ? 40 : -40}deg)`;
  }
}

function move(index) {
  if (index < 0) index = choiceYearData.length - 1;
  if (index >= choiceYearData.length) index = 0;
  currIndex.value = index;
  // 更改背景色
  (async () => {
    bgColorsString.value = await updateImageColors(`https://kanekikeh.online/A/CoursePerformance/timeLineImg/${choiceYearData[currIndex.value].image}`);
    bgColorsString.value.join(', ')
  })();
}

function prev() {
  move(currIndex.value - 1);
}

function next() {
  move(currIndex.value + 1);
}

onMounted(() => {
  move(Math.floor(choiceYearData.length / 2));
  choiceYearData.forEach(element => {
    preloadImage(`https://kanekikeh.online/A/CoursePerformance/timeLineImg/${element.image}`)
  });
});
</script>

<template>

  <body :style="{ background: 'linear-gradient(' + bgColorsString + ')' }">
    <img class="closeIcon" src="/src/assets/images/close.png" alt="" @click="closeTemplate()">
    <div class="shell">
      <div class="shell_body">
        <div class="button">
          <div class="prev" @click="prev"><img
              src="https://kanekikeh.online/A/CoursePerformance/timeLineImg/left.png" />
          </div>
          <div class="next" @click="next"><img
              src="https://kanekikeh.online/A/CoursePerformance/timeLineImg/right.png" />
          </div>
        </div>
        <div class="shell_slider" :style="{ width: totalWidth + 'px', transform: sliderTransform }">
          <div class="item" v-for="(item, index) in choiceYearData" :key="index"
            :style="{ width: itemWidth + 'px', height: itemHeight + 'px' }">
            <div class="frame" :style="{ transform: itemTransform(index) }">
              <div class="box front"
                :style="{ backgroundImage: 'url(https://kanekikeh.online/A/CoursePerformance/timeLineImg/' + item.image + ')' }"
                @click="userChoice(item)">
                <h1>{{ item.year }}</h1>
                <span>{{ item.text }}</span>
              </div>
              <div class="box left"
                :style="{ backgroundImage: 'url(https://kanekikeh.online/A/CoursePerformance/timeLineImg/' + item.image + ')' }">
              </div>
              <div class="box right"
                :style="{ backgroundImage: 'url(https://kanekikeh.online/A/CoursePerformance/timeLineImg/' + item.image + ')' }">
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </body>
</template>

<style scoped lang="scss">
* {
  padding: 0;
  margin: 0;
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  background-size: cover;
  overflow: hidden;
  transition: background-image 0.7s ease-in-out;
  width: 100vw;
  height: 100vh;
}

.closeIcon {
  width: 30px;
  height: 30px;
  transform: rotate(45deg);
  position: absolute;
  top: 12%;
  right: 5%;
  cursor: pointer;
  transition: .5s;
  z-index: 9999;

  &:hover {
    transform: rotate(405deg);
    cursor: pointer;
  }
}

.shell {
  position: relative;
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  box-sizing: border-box;
  /* background: rgba(99, 99, 99, 0.8); */
}

.button {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 380px;
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: -70%;
}

.prev,
.next {
  transition: transform 0.25s ease;
  z-index: 99999;
  bottom: 5px;
}

.prev img,
.next img {
  width: 60px;
  height: 60px;
  cursor: pointer;
}

.shell_body {
  transform: scale(0.9);
  /* padding: 20px 0 150px 0; */
  padding: 0;
}

.shell_slider {
  position: relative;
  transition: transform 1s ease-in-out;
  background: transparent;
}

.item {
  position: relative;
  float: left;
  /* margin: 0; */
  margin-top: -10vh;
}

.frame {
  position: relative;
  width: 100%;
  height: 100%;
  transition: transform 1s ease-in-out;
  transform-style: preserve-3d;
}

.frame:after {
  content: "";
  position: absolute;
  bottom: -16%;
  width: 100%;
  height: 60px;
  background: #ffffff1c;
  box-shadow: 0px 0px 15px 5px #ffffff1c;
  transform: rotateX(90deg) translate3d(0px, -20px, 0px);
}

.box {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: absolute;
  width: 100%;
  height: 100%;
  border: 4px solid #fff;
  perspective: 1000px;
  transform-style: preserve-3d;
  cursor: pointer;
}

.box h1,
.box span {
  color: #fff;
  transform: translateZ(20px);
}

.box h1 {
  text-shadow: 0 0 30px #48a9d8;
  font-size: 2.8rem;
  margin-top: -55%;
  margin-left: -55%;
}

.box span {
  position: absolute;
  bottom: 20px;
  padding: 0 25px;
  text-shadow: 0 0 10px #48a9d8;
  font-weight: bold;
}

.front,
.left,
.right {
  box-shadow: 0 0 50px #ffffff;
  background-size: cover;
}

.right,
.left {
  top: 0;
  width: 60px;
  backface-visibility: hidden;
}

.left {
  left: 0;
  border-left-width: 5px;
  transform: translate3d(1px, 0, -60px) rotateY(-90deg);
  transform-origin: 0%;
}

.right {
  right: 0;
  border-right-width: 5px;
  transform: translate3d(-1px, 0, -60px) rotateY(90deg);
  transform-origin: 100%;
}
</style>